//
//  NewPassWord.m
//  reSearchDemo
//
//  Created by 科技 on 15/7/31.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "NewPassWord.h"

@interface NewPassWord ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
}
@end

@implementation NewPassWord

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor=[UIColor whiteColor];
    [self setRightBarButtonImage:LOADIMAGE(@"btn_more") highlightedImage:nil selector:@selector(menu)];
    
    UIView * actionbar=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 44)];
    self.navigationItem.titleView = actionbar;
    UILabel * lab=[[UILabel alloc]initWithFrame:CGRectMake(8, 0, 100, 40)];
    lab.text=@"找回密码";
    lab.font=[UIFont systemFontOfSize:20];
    lab.textColor=[UIColor whiteColor];
    [actionbar addSubview:lab];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_tableView];
    
    HTTPGlobals *status = [HTTPGlobals shareHTTPGlobal];
    NSDictionary *dic = @{@"newpass":@"123456",@"newpass2":@"123456"};
    [status PostHTTPRequest:@"MobilePort/Index/safe_editfogetpassword.html" parameters:dic finish:^(BOOL seccess, NSString *error, id obj) {
        if (seccess) {
            DLog(@"修改成功: %@",obj);
            DLog(@"data = %@",obj[@"data"]);
        }
    }];
}

-(void)menu {
    
}

-(UIView *)buildMainView {
    UIView *headView = [[UIView alloc] init];
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 30, 200, 30)];
    textLabel.text = @"重设置云平台登录密码:";
    [headView addSubview:textLabel];
    NSArray *titleArr = @[@"输入你的新密码:",@"再次输入新密码:",];
    NSString *str = titleArr[0];
    CGSize sourceSize=[str sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]}];
//    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(textLabel.frame), CGRectGetMaxY(textLabel.frame), sourceSize.width, 30)];
//    label.font = [UIFont systemFontOfSize:17];
//    label.text = @"输入你的旧密码:";
//    UITextField *oldPW_TF = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label.frame), CGRectGetMinY(label.frame), 150, 30)];
//    oldPW_TF.tag = 2000;
//    oldPW_TF.secureTextEntry = YES;
//    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(textLabel.frame), CGRectGetMaxY(textLabel.frame) - 5, 150, 1)];
//    line.backgroundColor = [UIColor blackColor];
//    [headView addSubview:label];
//    [headView addSubview:oldPW_TF];
//    [headView addSubview:line];
    for (int i = 0; i < 2; i++) {
        UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(textLabel.frame), CGRectGetMaxY(textLabel.frame) + 40*i, sourceSize.width, 30)];
        lab.text = titleArr[i];
        lab.font = [UIFont systemFontOfSize:17];
        UITextField *TField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(lab.frame), CGRectGetMinY(lab.frame) , 150, 30)];
        TField.tag = 1000+i;
        TField.secureTextEntry = YES;
        UIView *lane = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(TField.frame), CGRectGetMaxY(TField.frame) - 5, 150, 1)];
        lane.backgroundColor = [UIColor blackColor];
        [headView addSubview:lab];
        [headView addSubview:TField];
        [headView addSubview:lane];
    }
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake(CGRectGetMinX(textLabel.frame), CGRectGetMaxY(textLabel.frame) + 2*30 + 30 + 30, CGRectGetWidth(self.view.frame) - 2*CGRectGetMinX(textLabel.frame), 40);
    [sureBtn setTitle:@"确定" forState:0];
    [sureBtn setTitleColor:[UIColor blackColor] forState:0];
    [sureBtn setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    [sureBtn addTarget:self action:@selector(SureBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:sureBtn];
    
    headView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
    
    NSString *promptText = @"提示：密码必须大于六位，必须含有大小写字母组成";
    UILabel *promptLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(sureBtn.frame) + 30, CGRectGetWidth(headView.frame) - 40, 40)];
    promptLabel.textColor = [UIColor lightGrayColor];
    promptLabel.numberOfLines = 0;
    promptLabel.text = promptText;
    promptLabel.font = [UIFont systemFontOfSize:13];
    
    [headView addSubview:promptLabel];
    
    return headView;
}
//确定按钮
-(void)SureBtnAction:(UIButton *)sender {
    UITextField *TF_1 = (UITextField *)[self.view viewWithTag:1000];
    UITextField *TF_2 = (UITextField *)[self.view viewWithTag:1001];
    if (![TF_1.text isEqualToString:TF_2.text]) {
        UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"注意" message:@"两次输入不一致" delegate:nil cancelButtonTitle:@"退出" otherButtonTitles:nil, nil];
        [alert show];
    }
    //检测用户输入密码必须要大小写、数字组成,并且最大长度不超过18，最小长度不小于6
    NSString *text1 = TF_1.text;
    NSString * regularExpression = @"^(?![0-9]+$)(?![a-zA-Z]+$)(?![a-z0-9]+$)(?![A-Z0-9]+$)[a-zA-Z0-9]{6,18}";
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regularExpression];
    BOOL flag = [pre evaluateWithObject:text1];
    if (!flag) {
        UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"注意" message:@"输入格式有误!" delegate:nil cancelButtonTitle:@"退出" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    NSString *path = @"MobilePort/Index/safe_editfogetpassword.html";
    NSDictionary *parame = [NSDictionary dictionaryWithObjectsAndKeys:TF_1.text,@"newpass",TF_2.text,@"newpass2", nil];
    [[HTTPGlobals shareHTTPGlobal] PostHTTPRequest:path parameters:parame finish:^(BOOL seccess, NSString *error, id obj) {
        if (seccess) {
            DLog(@"obj = %@",obj);
            DLog(@"data = %@",obj[@"data"]);
        }
    }];

}

#pragma mark-
#pragma mark--tableviewDelegate--
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *indentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = [self buildMainView];
    return headView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    UIView *headView = [self tableView:tableView viewForHeaderInSection:section];
    
    return CGRectGetHeight(headView.frame);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
